package com.photoeditor.demo.ui.widget.oldify;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lyzirving
 * time        2019/1/8
 * email       lyzirving@sina.com
 * information
 */
public class OldifyDecorationContainer extends View {

    private RectF mRenderRect;
    private Paint mPaint;
    private List<OldifyDecoration> mDecorations;
    private boolean mCanDraw;

    public OldifyDecorationContainer(Context context) {
        this(context, null);
    }

    public OldifyDecorationContainer(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OldifyDecorationContainer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs();
    }

    private void initAttrs() {
        setBackgroundColor(getResources().getColor(android.R.color.transparent));
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDecorations = new ArrayList<>();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRenderRect = new RectF(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mDecorations == null || mDecorations.size() == 0) return;
        if (!mCanDraw) return;
        for (OldifyDecoration dec : mDecorations)
            dec.drawSelft(canvas, mPaint);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mDecorations != null && mDecorations.size() > 0) {
            for (OldifyDecoration dec : mDecorations) {
                dec.destroy();
            }
        }
    }

    public void clear() {
        if (mDecorations == null) mDecorations = new ArrayList<>();
        mDecorations.clear();
    }

    public void addOldifyDecoration(OldifyDecoration dec, float left, float top) {
        dec.translate2SpecifiedPoint(left, top);
        mDecorations.add(dec);
    }

    public Bitmap getDecoration() {
        if (mDecorations == null || mDecorations.size() == 0) return null;
        Bitmap res = Bitmap.createBitmap((int) mRenderRect.width(), (int) mRenderRect.height(), Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(res);
        for (OldifyDecoration dec : mDecorations) {
            dec.drawSelft(canvas, mPaint);
        }
        return res;
    }

    public void setCanDraw(boolean canDraw) {
        mCanDraw = canDraw;
    }

    public void requestRender() {
        if (getVisibility() != VISIBLE) setVisibility(VISIBLE);
        invalidate();
    }

    public void clearAndHide() {
        mDecorations.clear();
        if (getVisibility() == VISIBLE) setVisibility(INVISIBLE);
        invalidate();
    }

}
